package com.example.xiaomiao.Controller;

import com.example.xiaomiao.DO.Money_Record;
import com.example.xiaomiao.DTO.*;
import com.example.xiaomiao.Service.MoneyRecordService;
import com.example.xiaomiao.Util.JwtUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/moneyRecord")
public class MoneyRecordController {
    @Resource
    MoneyRecordService moneyRecordService;

    @PostMapping("/add")
    public Return add(HttpServletRequest request, @RequestBody MoneyAddRequestDTO moneyAddRequestData){
        String token = JwtUtil.getTokenFromCookie(request);
        String id = JwtUtil.extractUsername(token);
        int userId = Integer.parseInt(id.substring(8));
        return moneyRecordService.add(userId,moneyAddRequestData);
    }

    @PostMapping("delete")
    public Return delete(@RequestBody MoneyDeleteRequestDTO moneyDeleteRequestData){
        return moneyRecordService.delete(moneyDeleteRequestData);
    }

    @PostMapping("update")
    public Return update(@RequestBody Money_Record moneyRecord){
        return moneyRecordService.update(moneyRecord);
    }

    @PostMapping("get")
    public Return get(HttpServletRequest request, @RequestBody GetMoneyRequestDTO getMoneyRequestData){
        String token = JwtUtil.getTokenFromCookie(request);
        String id = JwtUtil.extractUsername(token);
        int userId = Integer.parseInt(id.substring(8));
        return moneyRecordService.get(userId,getMoneyRequestData);
    }
}
